/**
 * 2017，thor.com，all rights reserved。
 * 
 * Project：		palmmall-api
 * Filename：	Report.java
 * Created：		2017年4月27日
 * Creator：		wolv
 */
package com.thor.palmmall.api.rpt;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thor.commons.util.StringUtil;

/**
 * @author wolv
 *
 */
public class Report implements Serializable {
  private static final long serialVersionUID = -84420187925193702L;

  public BigDecimal getSingleValue() {
    return getItems().isEmpty() ? BigDecimal.ZERO : getItems().get(0).getValue();
  }

  public BigDecimal getTotalValue() {
    BigDecimal total = BigDecimal.ZERO;

    for (ReportItem item : items)
      total = total.add(item.getValue());

    return total;
  }

  public static Report readFrom(List<Object[]> rawData) {
    Report report = new Report();

    for (Object[] rawReport : rawData) {
      ReportItem item = new ReportItem();
      report.items.add(item);
      if (rawReport.length >= 1) {
        item.setDimension(StringUtil.toString(rawReport[0]));
      }

      if (rawReport.length >= 2) {
        item.setValue(
            rawReport[1] == null ? BigDecimal.ZERO : new BigDecimal(rawReport[1].toString()));
      }

      if (rawReport.length >= 3) {
        item.setDimensionId(StringUtil.toString(rawReport[2]));
      }
      
      if (rawReport.length >= 4) {
        item.setValue2(StringUtil.toString(rawReport[3]));
      }
    }
    return report;
  }

  public Map<String, ReportItem> dimensionMap() {
    Map<String, ReportItem> map = new HashMap<String, ReportItem>();
    if (items == null)
      return map;

    for (ReportItem item : items)
      map.put(item.getDimension(), item);

    return map;
  }

  private List<ReportItem> items = new ArrayList<ReportItem>();

  public List<ReportItem> getItems() {
    return items;
  }

  public void setItems(List<ReportItem> items) {
    this.items = items;
  }
}
